<template>
  <div class="link-list__item" v-for="item in list" :key="item.id">
    <div class="circle-link" :class="[item.isLight ? 'circle-link--light' : '']" v-if="item.type === 'circle'">
      <!-- 如果链接包含 http 则使用 a 标签跳转 -->
      <a v-if="item.url && item.url.includes('http')" :href="item.url" :class="[item.isLight ? 'nuxt-link-exact-active nuxt-link-active' : '']">
        <div>{{ item.text }}</div>
        <span class="circle-link__icon" :class="[item.isLight ? 'circle-link__icon--light' : '']"></span>
      </a>
      <!-- 否则使用 button 并 emit handleClick 事件 -->
      <button v-else @click="handleClick(item)" :class="[item.isLight ? 'nuxt-link-exact-active nuxt-link-active' : '']">
        <div>{{ item.text }}</div>
        <span class="circle-link__icon" :class="[item.isLight ? 'circle-link__icon--light' : '']"></span>
      </button>
    </div>
    <div class="text-link" v-else>
      <button type="button" class="text-link__wrapper link link--udrline-2" @click="handleClick(item)">
        <span class="text-link__text">{{ item.text }}</span>
      </button>
    </div>
  </div>
</template>

<script lang="ts" setup>
interface LinkItem {
  id: string | number
  type: 'circle' | 'text'
  text: string
  url?: string
  isLight?: boolean
}

defineProps<{
  list: LinkItem[]
}>()

const emit = defineEmits(['handleClick'])

const handleClick = (item: LinkItem) => {
  if (item.type === 'circle' && item.url && !item.url.includes('http')) {
    // circle 类型的内部链接
    emit('handleClick', item.url)
  } else if (item.type === 'text') {
    // text 类型的链接
    emit('handleClick', item)
  }
}
</script>

<style lang="scss" scoped>
/* 让 button 看起来像链接 */
.circle-link button {
  background: none;
  border: none;
  padding: 0;
  cursor: pointer;
  text-decoration: none;
  color: inherit;
  font: inherit;
  width: 100%;
  text-align: left;
}

/* 从 main.scss 迁移的 .link 样式 */
.link,
.link-body-p,
.link-caption,
.link-overline {
  display: flex;
  align-items: center;
  justify-content: center;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
  background-color: transparent;
  cursor: pointer;
  position: relative;
  text-decoration: none;
  color: #0048e8;
  color: var(--text-color, #0048e8);
}

.link-body-p:hover,
.link-caption:hover,
.link-overline:hover,
.link:hover {
  color: #0048e8;
  color: var(--text-color, #0048e8);
}

.link-overline {
  font-size: 14px;
  font-weight: 400;
  line-height: 1.285;
  letter-spacing: .06em;
  text-transform: uppercase;
  --text-color: #0048e8;
}

@media (max-width: 1023.98px) {
  .link-overline {
    font-size: 13px;
    line-height: 1;
  }
}

@media (max-width: 767.98px) {
  .link-overline {
    line-height: 1.166;
  }
}

.link-body-p {
  font-size: 16px;
  font-weight: 400;
  line-height: 1.75;
  --text-color: #0048e8;
}

@media (max-width: 1023.98px) {
  .link-body-p {
    font-size: 14px;
    line-height: 1.714;
    letter-spacing: .01em;
  }
}

.link-body-p.link--udrline:after {
  transform: scaleX(0) translateY(-.22em);
}

.link-body-p.link--udrline:hover:after {
  transform: scaleX(1) translateY(-.22em);
}

.link-caption {
  font-size: 14px;
  font-weight: 300;
  line-height: 1.214;
  --text-color: var(--text-color--primary);
}

@media (max-width: 1023.98px) {
  .link-caption {
    font-size: 13px;
    line-height: 1;
    letter-spacing: .04em;
  }
}

@media (max-width: 767.98px) {
  .link-caption {
    line-height: 1.33;
  }
}

.link--udrline {
  flex-direction: column;
}

.link--udrline--active:after,
.link--udrline:focus:after,
.link--udrline:hover:after {
  transform: scaleX(1);
}

.link--udrline:after {
  content: "";
  width: 100%;
  height: 1px;
  border-radius: 1px;
  background-color: #0048e8;
  background-color: var(--text-color, #0048e8);
  transform: scaleX(0);
  transition: all .3s ease-in-out;
}

.link--udrline-2 {
  display: inline-block;
}

.link--udrline-2 span {
  text-decoration: none;
  background-image: linear-gradient(90deg, #0048e8, #0048e8);
  background-image: linear-gradient(90deg, var(--text-color, #0048e8), var(--text-color, #0048e8));
  background-size: 0 1px, 100% 1px;
  background-position: 0 100%, 100% 100%;
  background-repeat: no-repeat;
  transition: background-size .3s;
  padding-bottom: 2px;
}

.link--udrline-2--active span,
.link--udrline-2:focus span,
.link--udrline-2:hover span {
  background-size: 100% 1px, 0 1px;
}

.link-list__item:not(:first-child) {
  margin-left: 40px;
}

@media (max-width: 767.98px) {
  .link-list__item:not(:first-child) {
    margin-left: 24px;
  }
}

.link-list.center .link-list__container {
  justify-content: center;
}

.text-link {
  display: flex;
}

.text-link__wrapper {
  background: none;
  padding: 0;
  border: none;
  --text-color: #0032a0;
}

.text-link--light .text-link__wrapper {
  --text-color: #fff;
}

.circle-link {
  display: flex;
  --link-color: var(--link-blue-color);
}

.circle-link--light {
  --link-color: var(--link-light-color);
}

.circle-link a,
.circle-link button {
  font-size: 14px;
  font-weight: 500;
  line-height: 1.285;
  letter-spacing: .04em;
  border: none;
  background: none;
  color: var(--link-color);
  text-decoration: none;
  display: flex;
  align-items: center;
  padding: 0;
}

@media (max-width: 1023.98px) {
  .circle-link a,
  .circle-link button {
    line-height: 1.428;
  }
}

@media (max-width: 767.98px) {
  .circle-link a,
  .circle-link button {
    line-height: 1.285;
  }
}

.circle-link a:hover .circle-link__icon:before,
.circle-link button:hover .circle-link__icon:before {
  transform: translate(-50%, -50%);
}

.circle-link a:hover .circle-link__icon:after,
.circle-link button:hover .circle-link__icon:after {
  transform: translate(150%, -50%);
}

.circle-link a:active .circle-link__icon:before,
.circle-link button:active .circle-link__icon:before {
  transform: translate(-260%, -50%);
}

.circle-link a:active .circle-link__icon:after,
.circle-link button:active .circle-link__icon:after {
  transform: translate(-50%, -50%);
}

.circle-link__icon {
  width: 18px;
  height: 18px;
  border-radius: 50%;
  overflow: hidden;
  background-color: var(--link-color);
  position: relative;
  margin-left: 7px;
  flex-shrink: 0;
}

@media (max-width: 767.98px) {
  .circle-link__icon {
    width: 12px;
    height: 12px;
    margin-left: 4px;
  }
}

.circle-link__icon:after,
.circle-link__icon:before {
  content: "";
  width: 6px;
  height: 9px;
  position: absolute;
  top: 50%;
  left: 55%;
  background-image: url();
  background-position: 50%;
  background-size: cover;
  background-repeat: no-repeat;
  background-color: hsla(0,0%,100%,0);
  transition: transform .3s ease-in-out;
}

@media (max-width: 767.98px) {
  .circle-link__icon:after,
  .circle-link__icon:before {
    width: 4.7px;
    height: 6px;
    background-image: url();
    background-position: 50%;
    background-size: cover;
    background-repeat: no-repeat;
    background-color: hsla(0,0%,100%,0);
  }
}

.circle-link__icon:before {
  transform: translate(-260%,-50%);
}

.circle-link__icon:after {
  transform: translate(-50%,-50%);
}

.circle-link__icon--light:after,.circle-link__icon--light:before {
  background-image: url();
  background-position: 50%;
  background-size: cover;
  background-repeat: no-repeat;
  background-color: hsla(0,0%,100%,0);
}

@media (max-width: 767.98px) {
  .circle-link__icon--light:after,.circle-link__icon--light:before {
    background-image:url();
    background-position: 50%;
    background-size: cover;
    background-repeat: no-repeat;
    background-color: hsla(0,0%,100%,0);
  }
}
</style>
